package com.manor.main.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.enums.PopupPosition
import com.lxj.xpopup.interfaces.OnSelectListener
import com.lxj.xpopup.util.XPopupUtils
import com.noober.background.drawable.DrawableCreator
import com.manor.common.CommonAppConfig
import com.manor.common.utils.DpUtil
import com.manor.common.utils.StringUtil
import com.manor.common.utils.ToastUtil
import com.manor.main.R
import com.manor.main.bean.HomeBatchBean
import com.manor.main.bean.HomeTxtBean
import kotlinx.android.synthetic.main.popup_home_batch.view.*
import kotlin.math.roundToInt

/**
 *
 * @version 1.0
 *
 * @date 2020/5/22  15:22
 */
class HomeBatchPopup(context: Context, val dataList: MutableList<HomeBatchBean>, val txtList :MutableList<HomeTxtBean>, val
listener:
OnSelectBatchListener): CenterPopupView(context) {

    private var mAdapter: MAdapter? = null

    override fun getImplLayoutId(): Int {
        return R.layout.popup_home_batch
    }

    override fun onCreate() {
        super.onCreate()
        initView()
        initRecycler()
    }

    private fun initView() {
        if (CommonAppConfig.getInstance()?.userBean?.sex?:"0"=="1")
            tvConfirm.text = "一键撩一下"
        else
            tvConfirm.text = "一键打招呼"
        ivDismiss.setOnClickListener { dismiss() }
        tvConfirm.setOnClickListener {
            if (StringUtil.isEmpty(getIds())) {
                ToastUtil.show("请至少选择一位用户")
                return@setOnClickListener
            }
            listener.onSelect(0, tvSayText.text.toString(), getIds())
            dismiss()
        }
        tvSayText.setOnClickListener {
            showPopup(it)
        }
    }

    private fun initRecycler() {
        mAdapter = MAdapter(R.layout.item_home_batch, dataList)
        mRecycler.layoutManager = androidx.recyclerview.widget.GridLayoutManager(context, 3, androidx.recyclerview.widget.GridLayoutManager.VERTICAL, false)
        mRecycler.setHasFixedSize(true)
        mRecycler.adapter = mAdapter
        mAdapter?.run {
            this.setOnItemClickListener { _, _, position ->
                dataList[position].isCheck = !dataList[position].isCheck
                this.notifyItemChanged(position)
            }
        }
    }

    private fun showPopup(v:View){
        XPopup.Builder(context)
                .atView(v)
                .popupAnimation(PopupAnimation.NoAnimation)//ScrollAlphaFromTop
                .popupPosition(PopupPosition.Bottom)
                .hasShadowBg(false)
                .asCustom(HomeSayPopup(context,
                        txtList
                , OnSelectListener { position, text ->
                    tvSayText.text = text
                }))
                .show()
    }

    private fun getIds(): String {
        var ids = ""
        for (bean in dataList) {
            if (bean.isCheck) {
                if (StringUtil.isEmpty(bean.id))
                    continue
                ids += if (StringUtil.isEmpty(ids)) bean.id else ",${bean.id}"
            }
        }
        return ids
    }


    override fun getBackground(): Drawable {
        return DrawableCreator.Builder()
                .setCornersRadius(DpUtil.dp2px(24).toFloat())
                .setSolidColor(Color.parseColor("#ffffff"))
                .build()
    }

    override fun getMaxWidth(): Int {
        return (XPopupUtils.getWindowWidth(context) * 0.85).roundToInt()
    }

    override fun getMaxHeight(): Int {
        return (XPopupUtils.getWindowHeight(context) * 0.85).roundToInt()
    }

    interface OnSelectBatchListener {
        fun onSelect(position: Int, text: String,ids :String)
    }

    private class MAdapter(layout: Int, data: MutableList<HomeBatchBean>) : BaseQuickAdapter<HomeBatchBean, BaseViewHolder>
    (layout, data) {
        override fun convert(helper: BaseViewHolder, item: HomeBatchBean) {
            item.run {
                helper.setVisible(R.id.ivCheck, isCheck)
                        .setText(R.id.tvNick, nickname)
                Glide.with(context)
                        .load(avatar)
                        .error(R.mipmap.defaulthead)
                        .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(DpUtil.dp2px(16))))
                        .into(helper.getView(R.id.ivImg))
            }
        }



    }

}